﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Data;
using JXT.PrimaryKey.Batman.Extensions;
using JXT.PrimaryKey.Batman.Domain.Models;
using JXT.PrimaryKey.Batman.Domain.Specifications;
using JXT.PrimaryKey.Batman.Domain.VirtualExchange.Models;
using JXT.PrimaryKey.Batman.Domain.VirtualExchange.Repositories;
using JXT.PrimaryKey.Batman.Domain.Repositories.MySql;
using JXT.PrimaryKey.Batman.Domain.Repositories.MySql.Expressions;
using JXT.PrimaryKey.Batman.Domain.Quotation.Models;
using JXT.PrimaryKey.Batman.Domain.Repositories;

namespace JXT.PrimaryKey.Batman.Repositories.VirtualExchange
{
    public class MySqlSecuritiesDetailRepository : MySqlRepositoryBase<SecuritiesDetail, int>, ISecuritiesDetailRepository
    {
        public MySqlSecuritiesDetailRepository(RepositoryContext context)
            : base((MySqlRepositoryContext)context)
        {
        }
        public IEnumerable<SecuritiesDetail> GetListByHoldingCount(IPagedSpecification<SecuritiesDetail> pagedSpecification)
        {
            if (pagedSpecification == null)
                throw new ArgumentException("条件不能为空");
            return GetList(pagedSpecification);
        }

        public IEnumerable<SecuritiesDetail> GetListByHoldingAccountCount(IPagedSpecification<SecuritiesDetail> pagedSpecification)
        {
            if (pagedSpecification == null)
                throw new ArgumentException("条件不能为空");
            return GetList(pagedSpecification);
        }

        public DateTime? GetLastDateTime()
        {
            string sql = " select max(`updatetime`) as updatetime from `securitiesdetail`";
            DateTime? result = null;
            using (var dr = Context.ExecuteDataReader(sql, null))
            {
                if (dr.Read())
                {
                    object obj = dr["updatetime"];
                    if (obj != DBNull.Value && obj != null)
                        result = Convert.ToDateTime(obj);
                }
            }
            return result;
        }





        //public void Test(SecuritiesDetail item)
        //{
        //    try
        //    {
        //        string sql = "INSERT INTO `securitiesdetail` (`markettype`, `securitiestype`, `code`, `holdingcount`, `holdaccountcount`, `updatetime`) VALUES (@markettype,@securitiestype,@code,@holdingcount,@holdaccountcount,@updatetime)";
        //        var parameters = new[]
        //        {
        //            Context.CreateParameter("@markettype",(int)item.MarketType),
        //            Context.CreateParameter("@securitiestype",(int)item.SecuritiesType),
        //            Context.CreateParameter("@code",item.Code),
        //            Context.CreateParameter("@holdingcount",item.HoldingCount),
        //            Context.CreateParameter("@holdaccountcount",item.HoldAccountCount),
        //            Context.CreateParameter("@updatetime",item.UpdateTime.Date)
        //        };
        //        Context.ExecuteNonQuery(sql, parameters);
        //    }catch(Exception ex){
        //        string a = string.Empty;
        //    }
        //}
    }
}
